package com.cat.model.web.action.admin;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;

import org.springframework.beans.factory.annotation.Autowired;

import com.alonew.core.commons.page.Page;
import com.cat.model.biz.service.admin.ModelInfoService;
import com.cat.model.common.model.common.DressupScalePictureVO;
import com.cat.model.common.model.common.ModelScalePictureVO;
import com.cat.model.common.util.FileUtil;
import com.cat.model.common.util.ImageUtil;
import com.cat.model.dao.common.PlmCommDAO;
import com.cat.model.dao.mapper.ModelMapper;
import com.cat.model.web.action.base.AdminAction;

public class ModelScalePicManagerAction extends AdminAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = -4900973661334137717L;
	private String modelId;
	private File image;
	private String imageFileName; 
	private File vedio;
	private String vedioFileName;
	private ModelScalePictureVO scalePic;
	private Long id;
	private String imageUrl;
	
	@Autowired
	private PlmCommDAO plmCommDAO;
	
	@Autowired
	private ModelInfoService modelInfoService; 
	
	@Autowired
	private ModelMapper modelMapper;
	
	@SuppressWarnings("unchecked")
	public String list() {
		Map<String, Object> condition = new HashMap<String, Object>();
		
		if(modelId != null){
			if(modelId.contains(",")){
				String[] strs = modelId.split(",");
				modelId = strs[0];
			}
			condition.put("modelId", modelId);
		}
		
		Page page = this.fetchPageParams();
		page.setNumPerPage(20);
		
		Long totalCount = plmCommDAO.searchSize("manager.searchModelScalePicSize", condition);
		page.setTotalCount(totalCount);
		int start = (page.getPageNum() -1) * page.getNumPerPage();
		if(start < 0){
			start = 0;
		}
		
		List<ModelScalePictureVO> list = (List<ModelScalePictureVO>)plmCommDAO.search("manager.searchModelScalePic", condition, start, page.getNumPerPage());
		if(list != null){
			for(ModelScalePictureVO bean : list){
				bean.setImage(ImageUtil.wrapImageUrl(bean.getImage()));
			}
		}
		
		request.setAttribute("list", list);
		request.setAttribute("size", totalCount);
		return "list";
	}
	
	public String deleteSingle() {
		modelInfoService.deleteModelScalePic(id);
		okAlert("删除成功！");
		return "list";
	}
	
	public String toAdd(){
		return "add";
	}
	
	public String add(){
		try {
			if(image != null){
				BufferedImage img = ImageIO.read(new FileInputStream(image));
				if(img == null){
					this.printHTML("<script>parent.callback(" + false + ", '"+"图片格式不正确"+"')</script>");
					return "list";
				}
			}
//			if(vedio != null){//都放开
//				String filePostfix = FileUtil.getFilePostfix(vedioFileName);
//				if(!(".MP4".equals(filePostfix)||".mp4".equals(filePostfix))){
//					this.printHTML("<script>parent.callback(" + false + ", '"+"请上传正确格式文件"+"')</script>");
//					return "list";
//				}
//			}
			modelInfoService.addModelScalePic(scalePic, image, imageFileName, vedio, vedioFileName);
		} catch (Exception e) {
			e.printStackTrace();
			parentAlert(false, e.getMessage());
			return "list";
		}
		parentAlert(true, "保存成功");
		return "list";
	}
	
	public String toEdit(){
		scalePic = modelMapper.getModelScalePic(id);
		if(scalePic != null){
			imageUrl = ImageUtil.wrapImageUrl(scalePic.getImage());
		}
		return "edit";
	}
	
	public String edit(){
		try {
			if(image != null){
				BufferedImage img = ImageIO.read(new FileInputStream(image));
				if(img == null){
					this.printHTML("<script>parent.callback(" + false + ", '"+"图片格式不正确"+"')</script>");
					return "list";
				}
			}
//			if(vedio != null){//都放开
//				String filePostfix = FileUtil.getFilePostfix(vedioFileName);
//				if(!(".MP4".equals(filePostfix)||".mp4".equals(filePostfix))){
//					this.printHTML("<script>parent.callback(" + false + ", '"+"请上传正确格式文件"+"')</script>");
//					return "list";
//				}
//			}
			modelInfoService.updateModelScalePic(scalePic, image, imageFileName, vedio, vedioFileName);
		} catch (Exception e) {
			e.printStackTrace();
			parentAlert(false, e.getMessage());
			return "list";
		}
		parentAlert(true, "保存成功");
		return "list";
	}

	public String getModelId() {
		return modelId;
	}

	public void setModelId(String modelId) {
		this.modelId = modelId;
	}

	public File getImage() {
		return image;
	}

	public void setImage(File image) {
		this.image = image;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	public File getVedio() {
		return vedio;
	}

	public void setVedio(File vedio) {
		this.vedio = vedio;
	}

	public String getVedioFileName() {
		return vedioFileName;
	}

	public void setVedioFileName(String vedioFileName) {
		this.vedioFileName = vedioFileName;
	}

	public ModelScalePictureVO getScalePic() {
		return scalePic;
	}

	public void setScalePic(ModelScalePictureVO scalePic) {
		this.scalePic = scalePic;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getImageUrl() {
		return imageUrl;
	}

	public void setImageUrl(String imageUrl) {
		this.imageUrl = imageUrl;
	}

}
